package com.address.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.test.address.OrderedProperties;

public class FileUtils {
	public static final String PATHPREFIX = "/address/Properties/";
	public static final String PATHSUFFIX = ".properties";
	public static final String UTF8 = "utf-8";
	public static final String FILESUFFIX = "properties";

	public static String getLocalPath() {
		return System.getProperty("user.dir");
	}

	public static String getAbsoluteFileName(String fileName) {
		return getAbsolutePath() + fileName + PATHSUFFIX;
	}
	
	public static String getAbsolutePath() {
		return getLocalPath() + PATHPREFIX;
	}

	public static Properties readAsUTF8(String fileName) {
		Properties properties = new OrderedProperties();
		InputStream inputStream = null;
		try {
			inputStream = new FileInputStream(getAbsoluteFileName(fileName));
			BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream, UTF8));
			properties.load(bf);
			properties.load(inputStream); /// 加载属性列表
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				inputStream.close();
			} catch (IOException e) {
				// ignore
			}
		}
		return properties;
	}

	public static void pro2String(Properties properties) {
		Iterator<String> it = properties.stringPropertyNames().iterator();
		while (it.hasNext()) {
			String key = it.next();
			System.out.println(key + ":" + properties.getProperty(key));
		}
	}
	
	public static String getValueByKey(File file , String field){
		String fileName = getFileNameWithoutExtension(file);
		Properties properties = readAsUTF8(fileName);
		Iterator<String> it = properties.stringPropertyNames().iterator();
		while (it.hasNext()) {
			if(field.equals(it.next())){
				return properties.getProperty(field);
			}		
		}
		return null;
	}

	public static void writeAsUTF8(String fileName, Map<String, String> messages) throws Exception {
		if(messages == null){
			createAsUTF8(fileName);
		}else{
			Properties properties = new Properties();
		try {
			OutputStream outputStream = new FileOutputStream(getAbsoluteFileName(fileName));
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream, UTF8));
			for (Map.Entry<String, String> message : messages.entrySet()) {
				properties.setProperty(message.getKey(), message.getValue());
			}
			properties.store(bw, null);
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		}
		
	}

	public static void createAsUTF8(String fileName) throws Exception {
		Properties properties = new Properties();
		try {
			OutputStream outputStream = new FileOutputStream(getAbsoluteFileName(fileName));
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream, UTF8));
			properties.store(bw, null);
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void appendAsUTF8(String fileName, Map<String, String> messages) throws Exception {
		if (messages == null) {
			createAsUTF8(fileName);
		} else {
			Properties properties = new Properties();
			try {
				OutputStream outputStream = new FileOutputStream(getAbsoluteFileName(fileName), true);
				BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream, UTF8));
				for (Map.Entry<String, String> message : messages.entrySet()) {
					properties.setProperty(message.getKey(), message.getValue());
				}
				properties.store(bw, null);
				outputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
	
	public static File[] getAllFiles(String path){
		File file=new File(path);
		return file.listFiles();
	}
	
	public static String getWholeFileName(File file){
		return file.getName();
	}

	public static String getFileNameWithoutExtension(File file){
		String wholeName = getWholeFileName(file);
		return wholeName.substring(0,wholeName.lastIndexOf("."));
	}
	
	public static String getExtension(File file){
		String wholeName = getWholeFileName(file);
		return wholeName.substring(wholeName.lastIndexOf(".")+1);
	}
	
	public static void deleteFile(File file){
		file.delete();
	}
	
	public static File[] getAllLocalFiles(){
		return getAllFiles(getAbsolutePath());
	}
	
	public static List<File> getAllLocalProperties(){
		List<File> listFile = new LinkedList<File>();
		File[] files = getAllLocalFiles();
		for(File file : files){
			if(getExtension(file).equals(FILESUFFIX)){
				listFile.add(file);
			}
		}
		return listFile;
	}
}
